/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package glamourbeautyclinic.service;

import images.GlamourImage;
import java.awt.Point;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JInternalFrame;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import util.DBConnection;

/**
 *
 * @author anh
 */
public class FrmOperationReport extends javax.swing.JInternalFrame {

    GlamourImage GI = new GlamourImage();
    DBConnection dc;
    ResultSet rs;
    Statement stmt;
    DefaultTableModel reportModel;

    /**
     * Creates new form FrmRelApp
     */
    public FrmOperationReport(DBConnection dc) {
        initComponents();
        this.dc = dc;
        GI.addIconFor(this);

        RowSorter<TableModel> sorter = new TableRowSorter<TableModel>(tblSerApp.getModel());
        tblSerApp.setRowSorter(sorter);
        tblSerApp.setComponentPopupMenu(pmnSerApp);

        //Add data to table
        //serAppModel = new DefaultTableModel();
        reportModel = (DefaultTableModel) tblSerApp.getModel();
        reportModel.setRowCount(0);
        tblSerApp.setModel(reportModel);
        try {
            stmt = dc.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            String sql = "select CatName,ServiceName,SttDetail,Count(ServiceName) as counterNum "
                    + "from vw_full_app "
                    + "group by ServiceName , SttDetail, CatName";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                //if (!rs.getString("CatName").equals("")) {
                if (reportModel.getRowCount() == 0) {
                    reportModel.addRow(new Object[]{rs.getString(1), rs.getString(2), rs.getInt(4), 0, 0});

                } else {
                    boolean flag = false;
                    for (int i = 0; i < reportModel.getRowCount(); i++) {
                        if (rs.getString(2).equalsIgnoreCase((String) reportModel.getValueAt(i, 1))) {
                            switch (rs.getString("SttDetail")) {
                                case "Finished":
                                    reportModel.setValueAt(rs.getInt(4), i, 2);
                                    break;
                                case "Pending":
                                    reportModel.setValueAt(rs.getInt(4), i, 3);
                                    break;
                                case "Canceled":
                                    reportModel.setValueAt(rs.getInt(4), i, 4);
                                    break;
                            }
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        switch (rs.getString("SttDetail")) {
                            case "Finished":
                                reportModel.addRow(new Object[]{rs.getString(1), rs.getString(2), rs.getInt(4), 0, 0});
                                break;
                            case "Pending":
                                reportModel.addRow(new Object[]{rs.getString(1), rs.getString(2), 0, rs.getInt(4), 0});
                                break;
                            case "Canceled":
                                reportModel.addRow(new Object[]{rs.getString(1), rs.getString(2), 0, 0, rs.getInt(4)});
                                break;
                        }

                    }
                }
            }
            tblSerApp.setModel(reportModel);
            for (int i = 0; i < reportModel.getRowCount(); i++) {
                if (reportModel.getValueAt(i, 0)== null) {
                    reportModel.removeRow(i);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pmnSerApp = new javax.swing.JPopupMenu();
        mniRefresh = new javax.swing.JMenuItem();
        mniCancel = new javax.swing.JMenuItem();
        mniFinish = new javax.swing.JMenuItem();
        mniDelete = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        mniDetails = new javax.swing.JMenuItem();
        btnRefresh = new javax.swing.JButton();
        lblFinish = new javax.swing.JLabel();
        lblCancel = new javax.swing.JLabel();
        lblPending = new javax.swing.JLabel();
        jspSerApp = new javax.swing.JScrollPane();
        tblSerApp = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        mniRefresh.setText("Refresh");
        pmnSerApp.add(mniRefresh);

        mniCancel.setText("Cancel");
        pmnSerApp.add(mniCancel);

        mniFinish.setText("Finish");
        pmnSerApp.add(mniFinish);

        mniDelete.setText("Delete");
        pmnSerApp.add(mniDelete);
        pmnSerApp.add(jSeparator1);

        mniDetails.setText("Details");
        pmnSerApp.add(mniDetails);

        btnRefresh.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnRefresh.setForeground(new java.awt.Color(0, 51, 51));
        btnRefresh.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Refresh.png"))); // NOI18N
        btnRefresh.setMnemonic('a');
        btnRefresh.setText("Refresh");
        btnRefresh.setToolTipText("Check all select combo box");
        btnRefresh.setBorder(null);
        btnRefresh.setContentAreaFilled(false);
        btnRefresh.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        btnRefresh.setMargin(new java.awt.Insets(6, 6, 6, 6));
        btnRefresh.setName(""); // NOI18N
        btnRefresh.setOpaque(true);
        btnRefresh.setPreferredSize(new java.awt.Dimension(59, 22));
        btnRefresh.setRolloverEnabled(false);

        lblFinish.setForeground(new java.awt.Color(0, 51, 255));
        lblFinish.setText("Finished: 2");

        lblCancel.setForeground(new java.awt.Color(0, 51, 255));
        lblCancel.setText("Cancel: 1");

        lblPending.setForeground(new java.awt.Color(0, 51, 255));
        lblPending.setText("Pending: 1");

        setBackground(new java.awt.Color(255, 255, 255));
        setTitle("Beauty Clinic");
        setName("SerApp"); // NOI18N

        tblSerApp.setForeground(new java.awt.Color(0, 102, 51));
        tblSerApp.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Category", "Service Name", "Finished", "Pending", "Canceled"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblSerApp.getTableHeader().setReorderingAllowed(false);
        tblSerApp.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                tblSerAppMousePressed(evt);
            }
        });
        jspSerApp.setViewportView(tblSerApp);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("OPERATION REPORT");

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/Logo.jpg"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jspSerApp, javax.swing.GroupLayout.DEFAULT_SIZE, 476, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(29, 29, 29)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel2)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jLabel1)
                        .addGap(27, 27, 27))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                .addComponent(jspSerApp, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tblSerAppMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tblSerAppMousePressed
        Point point = evt.getPoint();
        int currentRow = tblSerApp.rowAtPoint(point);
        tblSerApp.setRowSelectionInterval(currentRow, currentRow);
    }//GEN-LAST:event_tblSerAppMousePressed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRefresh;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JScrollPane jspSerApp;
    private javax.swing.JLabel lblCancel;
    private javax.swing.JLabel lblFinish;
    private javax.swing.JLabel lblPending;
    private javax.swing.JMenuItem mniCancel;
    private javax.swing.JMenuItem mniDelete;
    private javax.swing.JMenuItem mniDetails;
    private javax.swing.JMenuItem mniFinish;
    private javax.swing.JMenuItem mniRefresh;
    private javax.swing.JPopupMenu pmnSerApp;
    private javax.swing.JTable tblSerApp;
    // End of variables declaration//GEN-END:variables
}
